package com.example.demo.controller;
import com.example.demo.exception.MyUncheckedException;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.dto.ExamPaperDTO;
import com.example.demo.pojo.dto.PageBankShowDTO;
import com.example.demo.pojo.entity.ExamFile;
import com.example.demo.pojo.entity.ExamPaper;
import com.example.demo.pojo.entity.ExamPaperFile;
import com.example.demo.pojo.vo.ExamPaperVO;
import com.example.demo.pojo.vo.PageBankShowVO;
import com.example.demo.service.imply.IExamPaperServiceImply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson2.JSON;
/**
 * @author fjh
 */
@RestController
@RequestMapping("/examPaper")
@Slf4j
@Api(tags = "卷子接口")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080"}, allowCredentials = "true")
public class ExamPaperController {
    private  final IExamPaperServiceImply examPaperService;
    @ApiOperation("建立试卷")
    @PostMapping("/create")
    public Result create(@RequestBody ExamPaperDTO examPaperDTO){
        System.out.println("接收到的json数据");
        System.out.println(examPaperDTO);
        String jsonString = JSON.toJSONString(examPaperDTO);
        System.out.println(jsonString);
        ExamPaper result=examPaperService.createPaper(examPaperDTO);
        return Result.success(result);
    }
    @ApiOperation("初始化建卷题目表")
    @PostMapping("/initBank")
    public Result create(PageBankShowDTO pageBankShow){
        System.out.println(pageBankShow);
        List<PageBankShowVO>result=new ArrayList<>();
        if(pageBankShow.isShowLabel()&&pageBankShow.isShowFile()){
            System.out.println("文件与label");
            result=examPaperService.initByFl(pageBankShow.getSubjectId(),pageBankShow.getCategoryId());
        }else if(pageBankShow.isShowFile()){
            System.out.println("文件");
            result=examPaperService.initByFile(pageBankShow.getSubjectId(),pageBankShow.getCategoryId());
        }else if(pageBankShow.isShowLabel()){
            System.out.println("题型");
            result=examPaperService.initByLabel(pageBankShow.getSubjectId(),pageBankShow.getCategoryId());
        }
        return Result.success(result);
    }
    @ApiOperation("删除试卷")
    @PostMapping("/delete")
    public Result delete(int paperId){
        boolean result=examPaperService.delete(paperId);
        return Result.success(result);
    }
    @ApiOperation("复制试卷")
    @PostMapping("/copy")
    public Result copyFile(int examPaperId,int goalExamFileId){
        System.out.println("复制"+examPaperId+"---"+goalExamFileId);
        ExamPaper subjectFile=examPaperService.copyExamPaper(examPaperId,goalExamFileId);
        return Result.success(subjectFile);
    }
    @ApiOperation("移动试卷")
    @PostMapping("/shift")
    public Result shiftFile(int examPaperId,int goalExamFileId){
        ExamPaperFile paperFile=examPaperService.shiftExamPaper(examPaperId,goalExamFileId);
        if(paperFile!=null){
            return Result.success(paperFile);
        }
        return Result.error("移动失败");
    }
    @ApiOperation("根据id查找试卷信息")
    @PostMapping("/searchById")
    public Result searchById(int examPaperId) throws MyUncheckedException {
        ExamPaperVO examPaper=examPaperService.searchById(examPaperId);
        if(examPaper==null){
            return Result.error("查询失败");
        }
        return Result.success(examPaper);
    }
    @ApiOperation("测试")
    @PostMapping("/test")
    public Result test(@RequestBody List<Integer>examPaperDTO){
        System.out.println(examPaperDTO);

        return Result.success();
    }
}
